# -*- coding: utf-8 -*-
"""
Created on Mon Nov  6 14:12:17 2017
## Written based on keras toolkit
## Model: 3 layers fully connected neural network
@author: YXL
"""
import keras
from keras.datasets import mnist
from keras.layers import Dense, Activation
import numpy as np

#%% Load data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1] * x_train.shape[2])
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1] * x_test.shape[2])
y_train = (np.arange(10) == y_train[:, None]).astype(int)
y_test = (np.arange(10) == y_test[:, None]).astype(int)
#%% Model definition
model = keras.models.Sequential()
#1st layer
model.add(Dense(input_dim = 28*28, units = 500))
model.add(Activation('sigmoid'))
#2nd layer
model.add(Dense(units = 500))
model.add(Activation('sigmoid'))
#Output layer
model.add(Dense(units = 10))
model.add(Activation('softmax'))
#Configuration
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
#Find the optimal network parameters
model.fit(x_train, y_train, batch_size = 100, epochs = 2)

#%% Testing
#case1:
score = model.evaluate(x_test, y_test)
print('Total loss on Testing Set:', score[0])
print('Accuracy of Testing Set:', score[1])
#0.9609 0.9568
#case2:
result = model.predict(x_test)

#from keras.utils import plot_model
#plot_model
#keras.utils.plot_model(model, to_file='model.png')